package widget.list;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by HKYS on 2016-7-28.
 */
public class RecyclerView extends android.support.v7.widget.RecyclerView {
    public RecyclerView(Context context) {
        super(context);
    }

    public RecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public static abstract class BaseAdapter<VH extends ViewHolder> extends ListView.Adapter<VH> {
        protected OnRecyclerViewListener onRecyclerViewListener;

        public OnRecyclerViewListener getOnRecyclerViewListener() {
            return onRecyclerViewListener;
        }

        public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
            this.onRecyclerViewListener = onRecyclerViewListener;
        }
    }

    public static class OnRecyclerViewListener<T> {

        public void onInnerViewClick(final View view, final T data, final int position) {

        }

        public void onInnerViewCheck(final View view, final T data, final int position, boolean isChecked) {

        }

        public void onItemClick(final T data, final int position) {

        }

        public boolean onItemLongClick(final T data, final int position) {
            return false;
        }
    }

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        Adapter adapter = getAdapter();
        if (getAdapter() instanceof ListViewAdapter) {
            adapter = ((ListViewAdapter) getAdapter()).getWrappedAdapter();
        }
        if (adapter == null)
            throw new RuntimeException("adapter is not set first");
        if (adapter instanceof BaseAdapter) {
            ((BaseAdapter) adapter).setOnRecyclerViewListener(onRecyclerViewListener);
        } else {
            throw new RuntimeException("adapter is not extends RecyclerView.BaseAdapter<T>");
        }
    }


    public static class RecyclerViewBorder extends ItemDecoration {

        private int v;
        private int h;

        public RecyclerViewBorder(int v, int h) {
            this.v = v;
            this.h = h;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, android.support.v7.widget.RecyclerView parent, State state) {
            final int position = parent.getChildPosition(view);
            final int count = parent.getAdapter().getItemCount();
            if (position == 0)
                outRect.top = v;
            else if (position == count - 1)
                outRect.bottom = v;
            outRect.left = h;
            outRect.right = h;
        }
    }


    public static class RecycleViewDivider extends ItemDecoration {

        private Paint mPaint;
        private Drawable mDivider;
        private int mDividerHeight = 2;//分割线高度，默认为1px
        private int mOrientation;//列表的方向：LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
        private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

        private boolean showTopDivider = false;
        private boolean showBottomDivider = true;

        public void setShowTopDivider(boolean showTopDivider) {
            this.showTopDivider = showTopDivider;
        }

        public void setShowBottomDivider(boolean showBottomDivider) {
            this.showBottomDivider = showBottomDivider;
        }

        /**
         * 默认分割线：高度为2px，颜色为灰色
         *
         * @param context
         * @param orientation 列表方向
         */
        public RecycleViewDivider(Context context, int orientation) {
            if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
                throw new IllegalArgumentException("请输入正确的参数！");
            }
            mOrientation = orientation;

//        final TypedArray a = context.obtainStyledAttributes(ATTRS);
//        mDivider = a.getDrawable(0);
//        a.recycle();
        }

        /**
         * 自定义分割线
         *
         * @param context
         * @param orientation 列表方向
         * @param drawableId  分割线图片
         */
        public RecycleViewDivider(Context context, int orientation, int drawableId) {
            this(context, orientation);
            mDivider = ContextCompat.getDrawable(context, drawableId);
            mDividerHeight = mDivider.getIntrinsicHeight();
        }

        /**
         * 自定义分割线
         *
         * @param context
         * @param orientation   列表方向
         * @param dividerHeight 分割线高度
         * @param dividerColor  分割线颜色
         */
        public RecycleViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {
            this(context, orientation);
            mDividerHeight = dividerHeight;
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(dividerColor);
            mPaint.setStyle(Paint.Style.FILL);
        }


        //获取分割线尺寸
        @Override
        public void getItemOffsets(Rect outRect, View view, android.support.v7.widget.RecyclerView parent, State state) {
            super.getItemOffsets(outRect, view, parent, state);
            final int position = parent.getChildPosition(view);
            final int count = parent.getAdapter().getItemCount();

            outRect.top = mDividerHeight;
            outRect.bottom = mDividerHeight;
            if (position == 0 && !showTopDivider) {
                outRect.top = 0;
            }
            if (position == count - 1 && !showBottomDivider) {
                outRect.bottom = 0;
            }
        }

        //绘制分割线
        @Override
        public void onDraw(Canvas c, android.support.v7.widget.RecyclerView parent, State state) {
            super.onDraw(c, parent, state);
            if (mOrientation == LinearLayoutManager.VERTICAL) {
                drawVertical(c, parent);
            } else {
                drawHorizontal(c, parent);
            }
        }

        //绘制横向 item 分割线
        private void drawHorizontal(Canvas canvas, android.support.v7.widget.RecyclerView parent) {
            final int left = parent.getPaddingLeft();
            final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
            final int childSize = parent.getChildCount();
            for (int i = 0; i < childSize; i++) {
                final View child = parent.getChildAt(i);
                LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
                final int top = child.getBottom() + layoutParams.bottomMargin;
                final int bottom = top + mDividerHeight;
                if (mDivider != null) {
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(canvas);
                }
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }

        //绘制纵向 item 分割线
        private void drawVertical(Canvas canvas, android.support.v7.widget.RecyclerView parent) {
            final int top = parent.getPaddingTop();
            final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
            final int childSize = parent.getChildCount();
            for (int i = 0; i < childSize; i++) {
                final View child = parent.getChildAt(i);
                LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
                final int left = child.getRight() + layoutParams.rightMargin;
                final int right = left + mDividerHeight;
                if (mDivider != null) {
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(canvas);
                }
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }
    }
}
